﻿namespace Blaze.Editor.UI
{
    using System;
    using UnityEditor;
    using UnityEngine.Rendering;

    /// <summary>
    /// 提供绘制<see cref="CustomBlendMode"/>的组件。
    /// </summary>
    public class CustomBlendModeDrawer
    {
        static CustomBlendModeDrawer()
        {
            mOptions = CustomBlendMode.All;
            mOptionStrings = new string[mOptions.Length];
            for (var i = 0; i < mOptions.Length; i++)
                mOptionStrings[i] = mOptions[i].FriendlyName;
        }

        public CustomBlendModeDrawer(string fieldName)
        {
            mFieldName = fieldName;
        }

        public CustomBlendMode Draw()
        {
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.PrefixLabel(mFieldName);
            mSelectedIndex = EditorGUILayout.Popup(mSelectedIndex, mOptionStrings);
            EditorGUILayout.EndHorizontal();
            if (mSelectedIndex < 0 || mSelectedIndex >= mOptions.Length)
                return CustomBlendMode.Undefined;
            var option = mOptions[mSelectedIndex];
            return option;
        }

        public void Select(BlendOp op, BlendMode src, BlendMode desc)
        {
            var option = CustomBlendMode.Parse(op, src, desc);
            mSelectedIndex = Array.IndexOf(mOptions, option);
        }

        private static readonly string[] mOptionStrings;
        private static readonly CustomBlendMode[] mOptions;
        private int mSelectedIndex;
        private readonly string mFieldName;
    }
}